import QQMapWX from '../../utils/qqmap-wx-jssdk.min'

let ins = getApp(), qqmap = new QQMapWX(ins.globalData.lbs)
Page({
  data: {
    map_ctx: null,
    selectOpen: ["兼职", "招聘"],
    _num: 0,
    role: "SEEKER",
    post: []
  },
  onLoad: function () {
    this.data.map_ctx = wx.createMapContext('home_map')
  },
  onShow() {
    this.clickControl()
    this.fetch_nearby()
  },
  //定位到我的位置
  clickControl() {
    this.data.map_ctx.moveToLocation()
  },
  //Switch
  switchChange(_ref) {
    let currentTarget = _ref.currentTarget,
        tab = currentTarget.dataset.index;
    
    if(tab == this.data._num) return false
    
    if (tab == "0") {
      this.setData({
        _num: 0,
        role: "SEEKER",
      })
    } else {
      this.setData({
        _num: 1,
        role: "EMPLOYER",
      })
    }

    this.fetch_nearby()
  },
  // 发布
  toPublish() {
    wx.navigateTo({url: '/pages/work/publish/publish'})
  },
  goSearch() {
    wx.navigateTo({url: '/pages/work/search/search?role=' + this.data.role})
  },
  fetch_nearby() {
    ins.api.requestApi('post/nearby', 'POST', {
      role: this.data.role,
      position: ins.globalData.position,
      range: 5000
    }, {
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          let markers = new Array(), data = resp.data.data;
          this.setData({
            post: data
          })

          for (const i in data) {
            markers.push({
              id: Number.parseInt(data[i].id),
              latitude: data[i].position.lat,
              longitude: data[i].position.lng,
              title: data[i].title,
              customCallout: {
                display: 'ALWAYS'
              },
              iconPath: '../images/location/weizhi.png',
              width: 40,
              height: 40,
            })
          }

          this.data.map_ctx.addMarkers({
            markers: markers,
            clear: true
          })
        } else {
          wx.showToast({
            title: resp.data.msg,
          })
        }
      }
    }, true)
  },
  toPostDetail(e) {
    wx.navigateTo({
      url: '/pages/post/detail?id=' + e.detail.markerId,
    })
  }
})


